//index.js
//获取应用实例
const app = getApp();
const util = require('../../utils/util.js')
Page( {
  data: {
    itemsCinemas: [], //影院列表 
    pageNumCinemas: 0,
    hasMoreCinemas: true,
    loading0: false,

    itemsShow: [], //热映
    pageNumHost: 0,
    hasMoreHost: true,
    loading1: false,

    itemsWait: [], //待映
    pageNumWait: 0,  
    hasMoreWait: true,
    loading2: false,

    pageSize: 10,

    swiperViewHeight: 0,
    currentTab: 0,
    city: '正在定位...',

    search: '',
  },
  swiperchange: function(e) {
    //FIXME: 当前页码
    //console.log(e.detail.current)
  },

  onLoad: function(parms) {
    var that = this
    //调用应用实例的方法获取全局数据
    that.cityInit();
    if(parms.currentTabFormHome) {
      this.setData({currentTab: parms.currentTabFormHome})
    }
  },
  search(e){
    const search = e.detail.value
    if (this.data.currentTab == 0) {
      this.setData({
        itemsCinemas: [], //影院列表 
        pageNumCinemas: 0,
        hasMoreCinemas: true,
        loading0: false,
        swiperViewHeight: 0,
      })
      this.getCinemasList(search);
    } else if (this.data.currentTab == 1) {
      this.setData({
        itemsShow: [], //热映
        pageNumHost: 0,
        hasMoreHost: true,
        loading1: false,
        swiperViewHeight: 0,
      })
      this.getMovieList(search);
    } else if (this.data.currentTab == 2) {
      this.setData({
        itemsWait: [], //待映
        pageNumWait: 0,  
        hasMoreWait: true,
        loading2: false,
        swiperViewHeight: 0,
      })
      this.getMovieWait(search);
    }
     
  },
  init(){
    this.getCinemasList();
    this.getMovieWait();  
    this.getMovieList();
  },
  getCinemasList: function(search) {
    // 影院列表
    var that = this;
    if(that.data.loading0) return

    that.data.loading0 = true;
    var pageNumCinemas = that.data.pageNumCinemas;
    var pageSize = that.data.pageSize;
    search = search || ''
    //playingList
    wx.request({
      url: app.globalData.url +`/movie/getCinemaList?search=${search}`,
      method: 'GET',
      data: {
        latitude: app.globalData.selectCity && app.globalData.selectCity.latitude,
        longitude: app.globalData.selectCity && app.globalData.selectCity.longitude,
        cityId: app.globalData.selectCity && app.globalData.selectCity.cityId,
        pageNum: ++pageNumCinemas,
        pageSize: pageSize
      },
      header: {
        'Accept': 'application/json'
      },
      success: function (res) {
        console.log(res.data.data);
        const itemsCinemas = that.data.itemsCinemas.concat(res.data.data.rows);
        that.setData({
          hasMoreCinemas: pageNumCinemas <= Math.ceil(res.data.data.total / 10),
          itemsCinemas,
          pageNumCinemas: pageNumCinemas
        })
        if (that.data.currentTab == 0) {
          that.setSwiperHeight();
        }
        that.data.loading0 = false;
      }
    })
  },
  getMovieList: function(search) {
    // 热映列表
    var that = this;
    if(that.data.loading1) return

    that.data.loading1 = true;
    var pageNumHost = that.data.pageNumHost;
    var pageSize = that.data.pageSize;
    search = search || ''
    //playingList
    wx.request({
      url: app.globalData.url +`/home/getMovieList?search=${search}`,
      method: 'GET',
      data: {
        addr: app.globalData.selectCity && app.globalData.selectCity.cityId,
        pageNum: ++pageNumHost,
        limit: pageSize
      },
      header: {
        'Accept': 'application/json'
      },
      success: function (res) {
        const itemsShow = that.data.itemsShow.concat(res.data.data.rows);
        that.setData({
          hasMoreHost: pageNumHost <= Math.ceil(res.data.data.total / 10),
          itemsShow,
          pageNumHost: pageNumHost
        })
        if (that.data.currentTab == 1) {
          that.setSwiperHeight();
        }
        that.data.loading1 = false;
      }
    })
  },
  getMovieWait: function(search) { 
    // 待映
    var that = this;
    if(that.data.loading2) return
    that.data.loading2 = true;
    var pageNumWait = that.data.pageNumWait;
    var pageSize = that.data.pageSize;
    search = search || ''
    wx.request({
      url: app.globalData.url +`/home/getMovieWait?search=${search}`,
      method: 'GET',
      data: {
        addr: app.globalData.selectCity && app.globalData.selectCity.cityId,
        pageNum: ++pageNumWait,
        limit: pageSize
      },
      success: function (res) {
        const itemsWait = that.data.itemsWait.concat(res.data.data.rows);
        that.setData({
          hasMoreWait: pageNumWait <= Math.ceil(res.data.data.total / 10),
          itemsWait,
          pageNumWait: pageNumWait
        })
        if (that.data.currentTab == 2) {
          that.setSwiperHeight();
        }
        that.data.loading2 = false;
      }
    })
  },
  onReachBottom: function(){
    if (this.data.currentTab == 0 && this.data.hasMoreCinemas)
      this.getCinemasList();
    else if (this.data.currentTab == 1 && this.data.hasMoreHost)
      this.getMovieList();
    else if (this.data.currentTab == 2 && this.data.hasMoreWait)
      this.getMovieWait();
  },
  cityInit:function() {
    if (app.globalData.userLocation) {
      this.setData({
        city: app.globalData.selectCity ? app.globalData.selectCity.cityName : '定位失败'
      })
      this.init();
    } else {
      app.userLocationReadyCallback = () => {
        this.setData({
          city: app.globalData.selectCity ? app.globalData.selectCity.cityName : '定位失败'
        })
        this.init();
      }
    }
  },
  setSwiperHeight: function(){
    var query = wx.createSelectorQuery();
    //选择id
    var that = this;
    var px1 = 208 / 750 * wx.getSystemInfoSync().windowWidth;
    var px2 = 376 / 750 * wx.getSystemInfoSync().windowWidth;
    query.selectAll('.swiperH').boundingClientRect(function (rect) {
      var itemsLength = 0;
      if (that.data.currentTab == 0){
        itemsLength = rect[0].height*that.data.itemsCinemas.length + 20;
      }else if(that.data.currentTab == 1){
        itemsLength = 116*that.data.itemsShow.length + 50;
      }else if(that.data.currentTab == 2){
        itemsLength = 116*that.data.itemsWait.length + 50;
      }
      that.setData({
        swiperViewHeight: itemsLength
      })
    }).exec();
  },
  //滑动切换
  swiperTab: function(e){
    var that = this;
    that.setSwiperHeight();
    wx.pageScrollTo({
      scrollTop: 0
    })
    that.setData({
      currentTab: e.detail.current
    });
  },
  //点击切换
  clickTab: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setSwiperHeight();
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  onShow: function () {
    if (app.globalData.selectCity) {
      this.setData({
        city: app.globalData.selectCity.cityName,
        itemsCinemas: [], //影院列表 
        itemsShow:[],
        itemsWait:[],

        pageNumCinemas: 0,
        pageNumHost:0,
        pageNumWait:0
      })
      this.init();
    }
  }
})
